package nl.ejvos.bgpgames.cbbchess.client;

import nl.ejvos.bgpgames.cbbchess.shared.MoveMessage;
import nl.ejvos.bgpgames.cbbchess.shared.OpponentMovedMessage;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.hu.bgp.framework.clientapi.Client;
import com.hu.bgp.framework.clientapi.ServerToClientMessage;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ChessClient extends Client
{
	private final VerticalPanel panel = new VerticalPanel();
	private Board board;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad2()
	{
		board = new Board(panel.getElement(), this);
	}

	protected Widget getPlayArea()
	{
		return panel;
	}

	protected void handleMessage2(ServerToClientMessage message)
	{
		if (message instanceof OpponentMovedMessage)
		{
			OpponentMovedMessage omm = (OpponentMovedMessage) message;
			if (!omm.getOpponentUuid().equals(getUserUuid()))
			{
				//Window.alert(omm.getToX() + " " + omm.getToY());
				//Square toSquare = board.m_squares[omm.getToX()][omm.getToY()];
				//Window.alert(omm.getX() + " " + omm.getY());
				//board.m_squares[omm.getX()][omm.getY()].getPiece().move(toSquare);
				Square toSquare = board.m_squares[omm.getToY()][omm.getToX()];
				board.m_squares[omm.getY()][omm.getX()].getPiece().move(toSquare);
			}
		}
	}

	public void onFailure(Throwable caught)
	{
		Window.alert(caught.getMessage());
	}

	public void broadcastMoveMessage(char x, int y, char toX, int toY)
	{
		MoveMessage message = new MoveMessage(getUserUuid(), x, y, toX, toY);
		sendMessageAndIgnoreOutcome(message);
	}
}
